<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="用户名称" prop="userId">
      <!--<el-input v-model="dataForm.userId" placeholder="用户名称"></el-input>-->
      <el-select v-model="dataForm.userId" placeholder="请选择用户">
        <el-option
          v-for="item in userList"
          :key="item.id"
          :label="item.userUsername"
          :value="item.id">
        </el-option>
      </el-select>

    </el-form-item>
    <el-form-item label="电表编号" prop="powerId">
      <el-input v-model="dataForm.powerId" placeholder="电表编号"></el-input>
    </el-form-item>
    <el-form-item label="电表跑数" prop="powerCount">
      <el-input v-model="dataForm.powerCount" placeholder="电表跑数"></el-input>
    </el-form-item>
    <el-form-item label="上月电表跑数" prop="powerBeforecount">
      <el-input v-model="dataForm.powerBeforecount" placeholder="上月电表跑数"></el-input>
    </el-form-item>
    <el-form-item label="抄表时间" prop="powerTime">
      <!--<el-input v-model="dataForm.powerTime" placeholder="抄表时间"></el-input>-->
      <el-date-picker
        v-model="dataForm.powerTime"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择抄表时间">
      </el-date-picker>
    </el-form-item>
    <!--<el-form-item label="电费缴费状态（待缴费，已缴费）" prop="powerStatus">-->
      <!--<el-input v-model="dataForm.powerStatus" placeholder="电费缴费状态（待缴费，已缴费）"></el-input>-->
    <!--</el-form-item>-->
    <el-form-item label="电费价格" prop="powerPriceid">
    <!-- <el-input v-model="dataForm.powerPriceid" placeholder="电费价格"></el-input> -->

     <el-select v-model="dataForm.powerPriceid" placeholder="请选择电价">
          <el-option
            v-for="item in powerList"
            :key="item.id"
            :label="item.powerprice"
            :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>
    <!--<el-form-item label="缴纳费用" prop="powerMoney">-->
      <!--<el-input v-model="dataForm.powerMoney" placeholder="缴纳费用"></el-input>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="支付时间" prop="powerPayTime">-->
      <!--<el-input v-model="dataForm.powerPayTime" placeholder="支付时间"></el-input>-->
    <!--</el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          powerId: '',
          powerCount: '',
          powerBeforecount: '',
          powerTime: '',
          // powerStatus: '',
          powerPriceid: '',
          powerMoney: '',
          powerPayTime: ''
        },
        powerList: [],
        userList: [],
        dataRule: {
          userId: [
            { required: true, message: '用户编号不能为空', trigger: 'blur' }
          ],
          powerId: [
            { required: true, message: '电表编号不能为空', trigger: 'blur' }
          ],
          powerCount: [
            { required: true, message: '电表跑数不能为空', trigger: 'blur' }
          ],
          powerBeforecount: [
            { required: true, message: '上月电表跑数不能为空', trigger: 'blur' }
          ],
          powerTime: [
            { required: true, message: '抄表时间不能为空', trigger: 'blur' }
          ],
          // powerStatus: [
          //   { required: true, message: '电费缴费状态（待缴费，已缴费）不能为空', trigger: 'blur' }
          // ],
          powerPriceid: [
            { required: true, message: '电费价格编号不能为空', trigger: 'blur' }
          ],
          powerMoney: [
            { required: true, message: '缴纳费用不能为空', trigger: 'blur' }
          ],
          powerPayTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.getAllList()
      this.getAllListpower()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/use/apppower/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.appPower.userId
                this.dataForm.powerId = data.appPower.powerId
                this.dataForm.powerCount = data.appPower.powerCount
                this.dataForm.powerBeforecount = data.appPower.powerBeforecount
                this.dataForm.powerTime = data.appPower.powerTime
                this.dataForm.powerStatus = data.appPower.powerStatus
                this.dataForm.powerPriceid = data.appPower.powerPriceid
                this.dataForm.powerMoney = data.appPower.powerMoney
                this.dataForm.powerPayTime = data.appPower.powerPayTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/use/apppower/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'powerId': this.dataForm.powerId,
                'powerCount': this.dataForm.powerCount,
                'powerBeforecount': this.dataForm.powerBeforecount,
                'powerTime': this.dataForm.powerTime,
                'powerStatus': this.dataForm.powerStatus,
                'powerPriceid': this.dataForm.powerPriceid,
                'powerMoney': this.dataForm.powerMoney,
                'powerPayTime': this.dataForm.powerPayTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取用户下拉框
      getAllList () {
        this.$http({
          url: this.$http.adornUrl('/use/appuser/listAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.userList = data.list
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      getAllListpower () {
              this.$http({
                url: this.$http.adornUrl('/use/appprice/listAll'),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.powerList = data.list
                } else {
                  this.dataList = []
                  this.totalPage = 0
                }
                this.dataListLoading = false
              })
            }
    }
  }
</script>
